package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.TableColumn;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.UI.Controllers.GenericDataDisplayerController;
import pl.edu.pw.MidEmu.UI.Views.GenericDataDisplayerView;

/**
 * Frame with GUI for displaying generic data grid with properties
 * @author Mateusz Starodab
 *
 */
public class GenericDataDisplayer extends BaseFrame<JDialog, GenericDataDisplayerController> implements GenericDataDisplayerView
{
	private JButton btnClose;

	private JTable dataTable;
	private MidasComponent parent;

	public GenericDataDisplayer(MidasComponent component, String title, Vector<Vector<String>> data)
	{
		GenericDataDisplayerController controller = new GenericDataDisplayerController();
		controller.setView(this);
		setController(controller);
		JDialog frame = new JDialog();
		parent = component;
		setComponent(frame);

		getComponent().setTitle(Consts.MainFrame_Title + " - " + parent.getName() + " - " + title);
		getComponent().setSize(500, 200);
		getComponent().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		getComponent().setModal(true);

		JPanel pnlContent = new JPanel(new BorderLayout());
		JPanel pnlCenter = new JPanel(new BorderLayout());
		JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.CENTER));

		Vector<String> collumns = new Vector<String>();
		collumns.add(Consts.DataDisplayer_Data);
		collumns.add(Consts.DataDisplayer_Value);

		dataTable = new JTable(data, collumns);

		TableColumn collumn1 = dataTable.getColumnModel().getColumn(0);
		collumn1.setPreferredWidth(150);

		TableColumn collumn2 = dataTable.getColumnModel().getColumn(1);
		collumn2.setPreferredWidth(400);

		JScrollPane scrollPane = new JScrollPane(dataTable);
		dataTable.setFillsViewportHeight(true);

		pnlCenter.add(dataTable.getTableHeader(), BorderLayout.NORTH);
		pnlCenter.add(scrollPane, BorderLayout.CENTER);

		btnClose = new JButton(Consts.DataDisplayer_Close);
		btnClose.setName(Consts.DataDisplayer_Close);
		btnClose.addActionListener(getController());
		pnlSouth.add(btnClose);

		pnlContent.add(pnlCenter, BorderLayout.CENTER);
		pnlContent.add(pnlSouth, BorderLayout.SOUTH);

		getComponent().add(pnlContent);
	}

	@Override
	public void close()
	{
		getComponent().setVisible(false);
		getComponent().dispose();
	}
}
